
// Definition for a binary tree node.
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

#include <algorithm>
#include <queue>
using namespace std;
class Solution
{
public:
    vector<int> largestValues(TreeNode *root)
    {
        vector<int> res;
        if (!root)
            return res;
        queue<TreeNode *> que;
        que.push(root);
        // //判断当前在哪一层 下层有多少 这层有多少
        // int i=0;
        // int level_num=1;
        // int next_level_num=0;
        // int level_max=INT_MIN;
        while (!que.empty())
        {
            // 获取这层有多少节点
            int num = que.size();
            // 遍历这层
            int level_max = INT_MIN;
            while (num--)
            {
                TreeNode *node = que.front();
                que.pop();
                int node_val = node->val;
                if (node->left)
                {
                    que.push(node->left);
                }
                if (node->right)
                {
                    que.push(node->right);
                }
                level_max = max(node_val, level_max);
            }
            // 更新答案
            res.push_back(level_max);
        }
        return res;
    }
};